package com.cy.milktea.utils;

import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;


/**
 * 图片处理工具类,后期考虑做成分布式服务器
 */
public class ImageUtils {

    private static final String imageDirAbsolutePath = System.getProperty("user.dir") + "-web/static/";  //-->图片目录的绝对路径
    private static final String webQuoteDirPath = "./static/";  //-->前端引用图片的前缀路径

    /**
     * 生成图片名称
     */
    private static String createImageName(String fileType) {
        return new SimpleDateFormat("yyyy-MM-dd-HH+").format(new Date())
                + UUID.randomUUID().toString()
                + "." + fileType;
    }

    /**
     * 创建图片绝对路径
     */
    private static String createImageAbsolutePath(String imageName) {
        return imageDirAbsolutePath + imageName;
    }

    /**
     * 创建图片相对路
     */
    private static String createImageRelativePath(String imageName) {
        return webQuoteDirPath + imageName;
    }

    /**
     * 根据图片的url获取图片的绝对路径
     */
    private static String getImageAbsolutePath(String imageUrl) {
        return imageDirAbsolutePath + imageUrl.replaceFirst(webQuoteDirPath, "");
    }

    /**
     * 通过图片名称或图片的路径获取图片类型
     */
    private static String getImageType(String image) {
        return image.substring(image.lastIndexOf(".") + 1);
    }

    /**
     * 删除图片
     */
    public static void deleteImage(String imageUrl) {
        if (StringUtils.hasLength(imageUrl)) {
            File file = new File(getImageAbsolutePath(imageUrl));
            if (file.exists()) file.delete();
        }
    }

    /**
     * 保存图片并返回图片url路径
     */
    public static String saveImage(MultipartFile file) {
        if (file == null || file.isEmpty()) return null;
        FileOutputStream out = null;
        try {
            String imgName = createImageName("jpg");
            out = new FileOutputStream(createImageAbsolutePath(imgName));
            out.write(file.getBytes());
            return createImageRelativePath(imgName);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 覆盖图片(删除+添加)
     */
    public static void coverImage(String imageUrl, MultipartFile file) {
        if (!StringUtils.hasLength(imageUrl)) return;
        if (file == null) return;
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(getImageAbsolutePath(imageUrl));
            out.write(file.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 克隆图片返回新的图片相对路径
     */
    public static String cloneImage(String imageUrl) {
        if (!StringUtils.hasLength(imageUrl)) return null;
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(getImageAbsolutePath(imageUrl));
            String iamgeName = createImageName(getImageType(imageUrl));
            fos = new FileOutputStream(createImageAbsolutePath(iamgeName));
            byte[] body = new byte[1024 * 10];
            int len = 0;
            while ((len = fis.read(body)) != -1) {
                fos.write(body, 0, len);
            }
            return createImageRelativePath(iamgeName);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) fis.close();
                if (fos != null) fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
























